import { ref, reactive, watchEffect } from 'vue';
import { useRouter } from "vue-router";
import * as Store from '@/store';
// import * as Api from '@/api/readPathology/readPathology';
import * as Api from '@/api/doctor/doctor';
import * as ApiType from '@/api/doctor/doctor.d';

interface TaskTableData {
  seriesUid: string;
  alrCount: number;
  submitTime?: number | null;
  status: number;
  userName?: string;
}

export default function usePageTable () {
  const router = useRouter();
  const TaskInfo = Store.useTaskInfo();
  const columns = ref([
    {
      title: '序列号',
      dataIndex: 'groupId',
      // width: 320,
      ellipsis: true,
      tooltip: true,
    },
    {
      title: '病灶数量',
      dataIndex: 'alrCount',
      // width: 180,
    },
    {
      title: '标注状态',
      dataIndex: 'status',
      slotName: 'status',
      // width: 180,
    },
    {
      title: '是否废片',
      dataIndex: 'status',
      slotName: 'showStatus',
      // width: 180,
    },
    {
      title: '标注医生',
      dataIndex: 'userName',
      // width: 240,
    },
    {
      title: '提交时间',
      dataIndex: 'submitTime',
      slotName: 'submitTime',
      // width: 280,
    },
    {
      title: '操作',
      dataIndex: 'action',
      slotName: 'action',
      // width: 203,
      // fixed: 'right'
    }
  ]);

  const scroll = reactive({
    x: '120%'
  })

  const tableData = ref<TaskTableData[]>([]);

  const total = ref(0);

  const searchForm = ref({
    seriesUid: '',
    userId: '',
    page: 1,
    pageSize: 10
  });

  const getTableData = async () => {
    const params = {
      ...searchForm.value,
      seriesUid: searchForm.value.seriesUid !== '' ? searchForm.value.seriesUid : undefined,
      userId: searchForm.value.userId !== '' ? +searchForm.value.userId : undefined,
      taskId: TaskInfo.taskId
    }
    const result = await Api.taskSeriesSearch(params);
    result && (tableData.value = reactive(result.data.list));
    result && (total.value = ref(result.data.total).value);
  };

  const handleSearch = () => {
    searchForm.value.page = 1;
    searchForm.value.seriesUid && (searchForm.value.seriesUid = searchForm.value.seriesUid.trim());
    searchForm.value.userId && (searchForm.value.userId = searchForm.value.userId);
    getTableData();
  };

  const handleReset = () => {
    searchForm.value = {
      seriesUid: '',
      userId: '',
      page: 1,
      pageSize: 10
    };
    getTableData();
  };

  // 页码操作
  const handleSizeChange = (size: number) => {
      searchForm.value.page = size;
      getTableData();
  };

  // 每页条数操作
  const handlePageSizeChange = (pageSize: number) => {
      searchForm.value.pageSize = pageSize;
      getTableData();
  };

  const handleQueryAll = () => {
    jumpPage('multiple');
  }

  const handleQueryResult = (item: ApiType.TaskSeriesSearchList) => {
    sessionStorage.setItem("task_info", JSON.stringify(item));
    jumpPage('once');
  };

  const jumpPage = (type: string) => {
    switch (TaskInfo.imageLayout) {
      case 'MPATHOLOGYPR_ANNO':
        // 大图
        router.push({
          path: '/bigPicture',
          query: {
            id: TaskInfo.taskId,
            type: 'query',
            dataType: type
          }
        })
        // window.open(`${window.location.origin}#/bigPicture?id=${TaskInfo.taskId}&type=query&dataType=${type}`,`${TaskInfo.taskId}`);
        break;
      case 'MPR_ANNO':
        // mpr
        router.push({
          path: '/mpr',
          query: {
            id: TaskInfo.taskId,
            type: 'query',
            dataType: type
          }
        })
        // window.open(`${window.location.origin}#/mpr?id=${TaskInfo.taskId}&type=query&dataType=${type}`,`${TaskInfo.taskId}`);
        break;
      case 'VIDEO_ANNO':
        router.push({
          path: '/video',
          query: {
            id: TaskInfo.taskId,
            type: 'query',
            dataType: type
          }
        })
        break;
      default:
        break;
    }
  }

  return {
    columns,
    tableData,
    total,
    searchForm,
    scroll,
    getTableData,
    handleSearch,
    handleReset,
    handleQueryAll,
    handleQueryResult,
    handleSizeChange,
    handlePageSizeChange
  }
}